# coding=utf-8
# 导入python包
import argparse
import cv2
from cv2 import circle
import numpy as np

# 读取彩色图片
image = cv2.imread('/home/zane/my_RL/MADDPG_torch/test/screenshot1.png')
output = image.copy()
# 将其转换为灰度图片

image_pro = cv2.Canny(image, 100, 200, 5)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# ret,binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# _,contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# for i in range(len(contours)):
# 	cnt = contours[i]
# 	x1, y1, w, h = cv2.boundingRect(cnt)
# 	print(x1, y1)
# 	print('---------------------------')

# 应用hough变换进行圆检测
circles = cv2.HoughCircles(image_pro, cv2.HOUGH_GRADIENT, 1, 10,  param1=100,param2=10,minRadius=0,maxRadius=25)



# 确保至少发现一个圆
if circles is not None:
	# 进行取整操作
	circles = np.round(circles[0, :]).astype("int")
	# 循环遍历所有的坐标和半径
	for (x, y, r) in circles:
		# 绘制结果
		print(circles[0])
		print((x/800)-0.5)
		print(-((y/800)-0.5))
		print(r/800)
		print('-------')
		cv2.circle(output, (x, y), r, (0, 255, 0), 4)
		cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)


	# 显示结果
	cv2.imshow("output", np.hstack([image, output]))
	cv2.imshow('canny', image_pro)
	cv2.imwrite('./original.jpg', image)
	cv2.imwrite('./canny.jpg', image_pro)
	cv2.imwrite('./output.jpg', output)
	cv2.waitKey(0)
else:
    print('error')

